//
// File: detrend.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 25-Apr-2025 12:42:26
//

// Include Files
#include "detrend.h"
#include "qrsolve.h"
#include "rt_nonfinite.h"
#include "xgeqp3.h"
#include "coder_array.h"
#include <cmath>
#include <string.h>

// Function Declarations
static void minus(coder::array<double, 1U> &in1,
                  const coder::array<double, 1U> &in2);

// Function Definitions
//
// Arguments    : coder::array<double, 1U> &in1
//                const coder::array<double, 1U> &in2
// Return Type  : void
//
static void minus(coder::array<double, 1U> &in1,
                  const coder::array<double, 1U> &in2)
{
  coder::array<double, 1U> b_in1;
  int i;
  int loop_ub;
  int stride_0_0;
  int stride_1_0;
  if (in2.size(0) == 1) {
    i = in1.size(0);
  } else {
    i = in2.size(0);
  }
  b_in1.set_size(i);
  stride_0_0 = (in1.size(0) != 1);
  stride_1_0 = (in2.size(0) != 1);
  if (in2.size(0) == 1) {
    loop_ub = in1.size(0);
  } else {
    loop_ub = in2.size(0);
  }
  for (i = 0; i < loop_ub; i++) {
    b_in1[i] = in1[i * stride_0_0] - in2[i * stride_1_0];
  }
  in1.set_size(b_in1.size(0));
  loop_ub = b_in1.size(0);
  for (i = 0; i < loop_ub; i++) {
    in1[i] = b_in1[i];
  }
}

//
// Arguments    : ::coder::array<double, 1U> &x
// Return Type  : void
//
namespace coder {
void detrend(::coder::array<double, 1U> &x)
{
  array<double, 2U> A;
  array<double, 2U> W;
  array<double, 1U> a;
  array<double, 1U> s;
  array<int, 2U> y;
  double tau_data[2];
  int n;
  int yk;
  if (x.size(0) - 1 < 0) {
    n = 0;
  } else {
    n = x.size(0);
  }
  y.set_size(1, n);
  if (n > 0) {
    y[0] = 0;
    yk = 0;
    for (int k{2}; k <= n; k++) {
      yk++;
      y[k - 1] = yk;
    }
  }
  s.set_size(y.size(1));
  n = y.size(1);
  for (int k{0}; k < n; k++) {
    s[k] = y[k];
  }
  if (x.size(0) != 0) {
    if (x.size(0) == 1) {
      n = x.size(0);
      for (int k{0}; k < n; k++) {
        x[k] = x[k] * 0.0;
      }
    } else {
      double p[2];
      double tol;
      int jpvt[2];
      int r;
      yk = s.size(0) - 1;
      a.set_size(s.size(0));
      for (int k{0}; k <= yk; k++) {
        a[k] = s[k] - s[0];
      }
      n = a.size(0);
      for (int k{0}; k < n; k++) {
        a[k] = a[k] / s[s.size(0) - 1];
      }
      n = a.size(0);
      for (int k{0}; k < n; k++) {
        tol = a[k];
        a[k] = std::fmax(tol, 0.0);
      }
      W.set_size(s.size(0), 2);
      for (int k{0}; k <= yk; k++) {
        W[k] = a[k];
        W[k + W.size(0)] = 1.0;
      }
      A.set_size(W.size(0), 2);
      n = W.size(0) * 2;
      for (int k{0}; k < n; k++) {
        A[k] = W[k];
      }
      internal::lapack::xgeqp3(A, tau_data, &n, jpvt);
      r = 0;
      if (A.size(0) < 2) {
        n = A.size(0);
        yk = 2;
      } else {
        n = 2;
        yk = A.size(0);
      }
      if (n > 0) {
        tol = std::fmin(1.4901161193847656E-8,
                        2.2204460492503131E-15 * static_cast<double>(yk)) *
              std::abs(A[0]);
        while ((r < n) && (!(std::abs(A[r + A.size(0) * r]) <= tol))) {
          r++;
        }
      }
      s.set_size(x.size(0));
      n = x.size(0) - 1;
      for (int k{0}; k <= n; k++) {
        s[k] = x[k];
      }
      internal::LSQFromQR(A, tau_data, jpvt, s, r, p);
      n = W.size(0);
      s.set_size(W.size(0));
      for (int k{0}; k < n; k++) {
        s[k] = W[k] * p[0] + W[W.size(0) + k] * p[1];
      }
      if (x.size(0) == s.size(0)) {
        n = x.size(0);
        for (int k{0}; k < n; k++) {
          x[k] = x[k] - s[k];
        }
      } else {
        minus(x, s);
      }
    }
  }
}

} // namespace coder

//
// File trailer for detrend.cpp
//
// [EOF]
//
